# 一些常用的时间有关的工具
import datetime
import time


def time_consumed(fn):
    """计算方法耗时的装饰器"""
    def _wrapper(*args, **kwargs):
        start = time.time()
        res = fn(*args, **kwargs)
        seconds = time.time() - start
        print(u"函数:<{func}> 执行耗时：{sec}秒".format(func=fn.__name__, sec=seconds))
        return res
    return _wrapper


def get_current_time():
    """获取当前时间字符串"""
    # current_time = time.strftime('%Y-%m/%d', time.localtime(time.time()))  # 返回当前时间
    timeObj = datetime.datetime.now()
    current_time = timeObj.strftime('%Y-%m/%d-') + ('am' if timeObj.hour <= 12 else 'pm')
    return current_time


def get_real_time(input_date):
    """
    统一不同格式的时间字符串为 一个格式
    获取几秒钟前、几分钟前、几小时前、几天前，几个月前、及几年前的具体时间
    :param delta_time 格式如：50秒前，10小时前，31天前，5个月前，8年前
    :return: 具体时间 %Y-%m-%d
    """
    list_str = ["秒前", "秒钟前", "分钟前", "小时前", "天前", "个月前", "月前", "年前", '昨天', '前天', '今天',
                '刚刚']
    if any([i in input_date for i in list_str]):
        if '昨天' in input_date:
            return time.strftime('%Y-%m-%d', time.localtime(time.time() - 86400))
        elif '前天' in input_date:
            return time.strftime('%Y-%m-%d', time.localtime(time.time() - 172800))
        elif '今天' in input_date:
            return time.strftime('%Y-%m-%d')
        elif '刚刚' in input_date:
            return time.strftime('%Y-%m-%d')

    # 将输入的字符串转换为日期对象
    date_object = None

    formats = ["%Y-%m-%d", "%y-%m-%d", "%Y/%m/%d", "%y/%m/%d", "%Y.%m.%d", "%m.%d", "%Y年%m月%d日", "%m月%d日",
               "%Y-%m-%dT%H:%M:%S.%f%z", "%Y%m%d", "%Y%m%d", "昨天%H:%M", "昨天 %H:%M", "今天 %H:%M",
               '%M秒前', '%M 秒前', '%M秒钟前', '%M 秒钟前', "今天%H:%M", '%M分钟前', '%M 分钟前', '%H小时前',
               '%H 小时前',
               "%m/%d %H:%M", "%H:%M", "%Y-%m-%d %H:%M:%S", "%m-%d %H:%M"]

    for fmt in formats:
        try:
            date_object = datetime.datetime.strptime(input_date, fmt)
            this_year = datetime.datetime.today().year
            if "1900" in date_object.strftime('%Y-%m-%d'):
                date_object = date_object.replace(year=this_year)
            if f"{this_year}-01-01" == date_object.strftime('%Y-%m-%d'):
                date_object = date_object.replace(month=datetime.datetime.today().month,
                                                  day=datetime.datetime.today().day)
            break
        except ValueError:
            pass

    if not date_object:
        return None

    # 将日期对象转换为目标格式
    return date_object.strftime('%Y-%m-%d')


if __name__ == '__main__':
    date1 = '2023-05-05'
    date2 = '20-9-05'
    date3 = '20/9/2'
    date4 = '20-10-05'
    date5 = '2020/10/05'
    date6 = '2020.10.05'
    date7 = '10.05'
    date8 = '2020-11-24'
    date9 = '2020-11-25 09:20:53'
    date10 = '2020年11月25日'
    date11 = '2020-12-23T16:05:35.000+0000'
    date12 = '20201225'
    date13 = '202025'
    date14 = '25分钟前'
    date15 = '11月25日'
    date16 = '昨天 21:54'
    date17 = '今天 15:11'
    date18 = '01/19 09:50'
    date19 = '09:50'
    date20 = '05-23 20:53'
    date21 = '25秒前'

    dates = [date1, date2, date3, date4, date5, date6, date7, date8, date9, date10,
             date11, date12, date13, date14, date15, date16, date17, date18, date19, date20, date21]

    for date in dates:
        print(f"{date} -> {get_real_time(date)}")
